package com.yq.ylmall.acl.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yq.ylmall.acl.mapper.RoleMapper;
import com.yq.ylmall.acl.mapper.UserRoleMapper;
import com.yq.ylmall.acl.service.RoleService;
import com.yq.ylmall.model.acl.Role;
import com.yq.ylmall.dto.acl.RoleQueryDto;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class RoleServiceImpl extends ServiceImpl<RoleMapper, Role> implements RoleService {
    @Autowired
    private UserRoleMapper userRoleMapper;

    //角色分页列表
    @Override
    public IPage<Role> selectPage(Page<Role> pageParam, RoleQueryDto roleQueryDto) {
        //获取条件值：角色名称
        String roleName = roleQueryDto.getRoleName();
        //创建条件构造器对象
        LambdaQueryWrapper<Role> wrapper = new LambdaQueryWrapper<>();
        //判断条件值是否为空
        if(!StringUtils.isEmpty(roleName)) {
            //封装条件
            wrapper.like(Role::getRoleName,roleName);
        }
        //调用mapper方法实现条件分页查询
        IPage<Role> pageModel = baseMapper.selectPage(pageParam, wrapper);
        return pageModel;
    }

    @Override
    public Map<String, Object> findAllRoles(Long userId) {
        // 查询所有的角色数据
        List<Role> roleList = baseMapper.selectList(null) ;

        // 查询当前登录用户的角色数据
        List<Long> sysRoles = userRoleMapper.findSysUserRoleByUserId(userId);
        Map<String , Object> resultMap = new HashMap<>() ;
        resultMap.put("allRolesList" , roleList) ;
        resultMap.put("sysUserRoles", sysRoles);
        return resultMap;
    }
}
